package com.jm.runner;

import com.jm.constants.SystemConstants;
import com.jm.domain.entity.Article;
import com.jm.service.ArticleService;
import com.jm.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class TestRunner implements CommandLineRunner {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArticleService articleService;

    @Override
    public void run(String... args) throws Exception {
        //1.项目启动，自动加载mysql{id:viewCount}数据到redis
        List<Article> articles = articleService.list();
        Map<String,Integer> viewCountMap=articles.stream()
                .collect(Collectors.toMap(article -> article.getId().toString(),
                        article -> article.getViewCount().intValue()));
        redisCache.setCacheMap(SystemConstants.ARTICLE_VIEW_COUNT,viewCountMap);
    }
}
